<template>
  <el-dialog v-model="visible" :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false"
    :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="rules" ref="dataFormRef" @keyup.enter="dataFormSubmitHandle()"
      label-width="120px">


      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="用户" prop="userId">
            <el-select class="w100" @change="jxcrtxr" v-model="dataForm.userId" placeholder="请选择用户" clearable
              filterable>
              <el-option v-for="item in xczxtrt" 
              :key="item.id" :label="item.realName" :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <!-- <el-col :span="12" v-if="dataForm.userId">
          <el-form-item label="身份证">
            <el-input v-model="dataForm.idCard" placeholder="身份证"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12"  v-if="dataForm.userId">
          <el-form-item label="发薪卡名" >
            <el-input v-model="dataForm.bankName" placeholder="发薪卡名"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12"  v-if="dataForm.userId">
          <el-form-item label="卡号" >
            <el-input v-model="dataForm.payCard" placeholder="卡号"></el-input>
          </el-form-item>
        </el-col>

        <el-col :span="12"  v-if="dataForm.userId">
          <el-form-item label="所属支行">
            <el-input v-model="dataForm.payCardBranch" placeholder="所属支行"></el-input>
          </el-form-item>
        </el-col> -->
      </el-row>

      <el-form-item label=" 交易金额" prop="balance">
        <el-input v-model="dataForm.balance" placeholder="金额 交易"></el-input>
      </el-form-item>
      <!-- <el-form-item label="公司id" prop="companyId">
        <el-input v-model="dataForm.companyId" placeholder="公司id"></el-input>
      </el-form-item>
      <el-form-item label="公司名称" prop="companyName">
        <el-input v-model="dataForm.companyName" placeholder="公司名称"></el-input>
      </el-form-item> -->
      <!-- <el-form-item label="转帐流水" prop="recordNo">
        <el-input v-model="dataForm.recordNo" placeholder="转帐流水"></el-input>
      </el-form-item> -->
 
      <!-- <el-form-item label="交易类型" prop="recordType">
        <el-radio-group v-model="dataForm.recordType">
              <el-radio label="1"  >工资</el-radio>
              <el-radio label="2"  >转账</el-radio>
              <el-radio label="3"  >借款</el-radio>
            </el-radio-group>
      </el-form-item> -->
      <!-- <el-form-item label="交易渠道" prop="recordChannel">
        <el-radio-group v-model="dataForm.recordChannel">
              <el-radio label="1"  >银行卡</el-radio>
              <el-radio label="2"  >微信</el-radio>
              <el-radio label="3"  >其他</el-radio>
            </el-radio-group>
      </el-form-item> -->
      <!-- <el-form-item label="审核" prop="isPass" v-if="dataForm.id">
      
        <el-radio-group v-model="dataForm.isPass">
              <el-radio label="2"  >拒绝</el-radio>
              <el-radio label="3"  >通过</el-radio>
            </el-radio-group>
      </el-form-item> -->
      <el-form-item label="转账截图" v-if="dataForm.isPass==3">
       <div class="xzcsretrt">
        <dimg :dximg="dataForm.recordImg" @getimg="xzczxtrt"></dimg>
       </div>
      </el-form-item>
      <el-form-item label="拒绝原因"  v-if="dataForm.isPass==2">
        <el-input rows="10"  v-model="dataForm.content" type="textarea" placeholder="请输入拒绝原因"></el-input>
      </el-form-item>
 
  
    </el-form>
    <template #footer>
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">确定</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { reactive, ref } from "vue";
import baseService from "@/service/baseService";
import { ElMessage } from "element-plus";
import { dxget } from "@/utils/utils";
const emit = defineEmits(["refreshDataList"]);
import dimg from "@/components/dimg.vue";
const xczxtrt = ref([])
const visible = ref(false);
const dataFormRef = ref();

const dataForm = reactive({
  id: '', userId: '', realName: '', idCard: '', payCard: '', bankName: '', balance: '', companyId: '', companyName: '', recordNo: '', type: '', recordType: '', recordChannel: '', recordTime: '', recordImg: '', isPass: '', createTime: '', updateTime: ''
});

const rules = ref({
  userId: [
    { required: true, message: '必填项不能为空', trigger: 'blur' }
  ],
  realName: [
    { required: true, message: '必填项不能为空', trigger: 'blur' }
  ],
  idCard: [
    { required: true, message: '必填项不能为空', trigger: 'blur' }
  ],
  payCard: [
    { required: true, message: '必填项不能为空', trigger: 'blur' }
  ],
  bankName: [
    { required: true, message: '必填项不能为空', trigger: 'blur' }
  ],
  balance: [
    { required: true, message: '必填项不能为空', trigger: 'blur' }
  ],
  companyId: [
    { required: true, message: '必填项不能为空', trigger: 'blur' }
  ],
  companyName: [
    { required: true, message: '必填项不能为空', trigger: 'blur' }
  ],
  recordNo: [
    { required: true, message: '必填项不能为空', trigger: 'blur' }
  ],
  type: [
    { required: true, message: '必填项不能为空', trigger: 'blur' }
  ],
  recordType: [
    { required: true, message: '必填项不能为空', trigger: 'blur' }
  ],
  recordChannel: [
    { required: true, message: '必填项不能为空', trigger: 'blur' }
  ],
  recordTime: [
    { required: true, message: '必填项不能为空', trigger: 'blur' }
  ],
  recordImg: [
    { required: true, message: '必填项不能为空', trigger: 'blur' }
  ],
  isPass: [
    { required: true, message: '必填项不能为空', trigger: 'blur' }
  ],
  createTime: [
    { required: true, message: '必填项不能为空', trigger: 'blur' }
  ],
  updateTime: [
    { required: true, message: '必填项不能为空', trigger: 'blur' }
  ]
});

const init = (id?: number) => {
  visible.value = true;
  dataForm.id = "";
  dataForm.recordType = "1"
  // 重置表单数据
  if (dataFormRef.value) {
    dataFormRef.value.resetFields();
  }

  if (id) {
    getInfo(id);
  }
};

// 获取信息
const getInfo = (id: number) => {
  baseService.get("/api/userbalancelog/" + id).then((res) => {
    Object.assign(dataForm, res.data);
  });
};

// 表单提交
const dataFormSubmitHandle = () => {
  dataFormRef.value.validate((valid: boolean) => {
    dataForm.type = 1
    if (!valid) {
      return false;
    }
   
    (!dataForm.id ? baseService.post : baseService.put)("/api/userbalancelog", dataForm).then((res) => {
      ElMessage.success({
        message: '成功',
        duration: 500,
        onClose: () => {
          visible.value = false;
          emit("refreshDataList");
        }
      });
    });
  });
};
const jmkxcf = async () => {

  const { list } = await dxget('/api/user/page', {
    limit: 999,
    isAuthentication:true
  })
  xczxtrt.value = list
}
jmkxcf()
defineExpose({
  init
});
const xzczxtrt = (data) => {
  dataForm.recordImg = data
}
const jxcrtxr = (data) => {
  const obj = xczxtrt.value.filter(a => a.id == data)[0]
  dataForm.realName = obj.payName
  dataForm.idCard = obj.idCard
  dataForm.payCard = obj.payCard
  dataForm.bankName = obj.payCardName
  dataForm.payCardBranch = obj.payCardBranch
}
</script>
